package com.kingwang.demo.springsecuritytokendemo.security;

import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class InMemoryTokenUserDetailsService implements TokenUserDetailsService, UserDetailsService {
    private Map<String, UserDetails> userDetailsMap = new ConcurrentHashMap<>();

    @Override
    public UserDetails loadUserByToken(String tokenSessionId) {
        return this.userDetailsMap.get(tokenSessionId);
    }

    public void saveUserDetails(String tokenSessionId, UserDetails userDetails) {
        this.userDetailsMap.put(tokenSessionId, userDetails);
    }

    /**
     * 模拟实现，不可用
     *
     * @param username
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = new User(username, "null", Collections.singletonList(new SimpleGrantedAuthority("ROLE_user")));

        return user;
    }
}
